<template>
    <scroll-view 
        :scroll-y="true"
        class="mz_detail_view">
        <view class="container">
            <view class="title el-after">
                当前病人信息
            </view>
            <view class="one">
                <view class="top">
                    <view>
                        挂号单：{{userData.rid}}
                    </view>
                    <view>
                        姓名：{{userData.name}}
                    </view>
                </view>
                <view class="top_one">
                    科室：{{userData.sectionRoom}}
                </view>
                <view class="top_one">
                    主治医生：{{userData.doctor}}
                </view>
                <view class="content">
                    <view>
                        性别：{{userData.sex}}
                    </view>
                    <view>
                        身份证号：{{userData.idcardNo}}
                    </view>
                </view>
                <view class="bottom">
                    <view>
                        总金额：
                        <u-input v-model="userData.money" :disabled="true"/>
                    </view>
                    <view>
                        支付金额：<u-input v-model="money"/>
                    </view>
                    <view>
                        找零：<u-input v-model="rMoney" :disabled="true"/>
                    </view>
                </view>
                <u-button
                    type="success"
                    @click="onPayUp"
                    :loading="loading">
                    门诊结账
                </u-button>
            </view>
            <view class="title el-after">
                当前病人消费明细
            </view>
            <view class="two">
                <u-table>
                    <u-tr>
                        <u-th>药品名称</u-th>
                        <u-th>药品单价</u-th>
                        <u-th>药品数量</u-th>
                    </u-tr>
                    <view class="table_container">
                        <u-tr 
                            v-for="(item,index) in userData.drug" 
                            :key="index">
                            <u-td>{{item.Dname}}</u-td>
                            <u-td>{{item.DsellPrice}}</u-td>
                            <u-td>{{item.num}}</u-td>
                        </u-tr>
                    </view>
                </u-table>
            </view>
            <u-toast ref="uToast" />
        </view>
    </scroll-view>
</template>

<script>
//门诊详情视图
import SelectOfficeInputCp from "@/components/public/SelectOfficeInput";
import {
    getNoPayUser,
    payUp,
    updateRegister,
    addZhuYuanTongJi,
} from "@/common/api";
export default {
    components:{
        SelectOfficeInputCp,
    },
    data() {
        return {
            loading:false,
            userData:{},  //用户详细数据
            money:0,  //支付的金额
        }
    },
    onLoad(option) {  //路由传参
        this.getNoPayUser(option.rid);
    },
    computed:{
        rMoney:{  //找零
            set(){
                return;
            },
            get(){
                return this.money - this.userData.money;
            },
        },
    },
    methods: {
        getNoPayUser(rid){  //根据卡号获取未付费病人信息
            getNoPayUser(
                {
                    rid,
                },
                this.$store.state.user.token,
            ).then(data=>{
                this.userData = data;
            });
        },
        onPayUp(){  //门诊结账
            if(this.rMoney<=0){
                this.$refs.uToast.show({
                    title: "金额不足",
                    position:'top',
                    type: 'error',
                });
                return;
            }
            this.loading = true;
            const rid = this.userData.rid;
            const Kid = this.userData.kid;
            const Sname = this.userData.sectionRoom;
            const ymoney = this.userData.money;
            const time = this.$dateTools.dateFormat("yyyy-MM-dd");
            const token =  this.$store.state.user.token;
            Promise.all([
                updateRegister(
                    {
                        Rid:rid,
                        Status:1,
                    },
                    token,
                ),
                addZhuYuanTongJi(
                    {
                        Kid,
                        Status:1,
                        Sname,
                        time,
                        ymoney,
                    },
                    token,
                ),
        
            ]).then(()=>{
                this.$refs.uToast.show({
                    title: "结账成功",
                    position:'top',
                    type: 'success',
                });
                setTimeout(()=>{
                    uni.navigateBack();
                },1000);
            }).catch((msg)=>{
                this.$refs.uToast.show({
                    title:msg,
                    position:'top',
                    type:'error',
                });
            }).finally(()=>{
                this.loading=false;
            });
        },
    },
}
</script>
<style scoped lang="scss">
    .mz_detail_view{
        height: calc(100vh);
        .container{
            padding: 35rpx;
            box-sizing: border-box;
            .title{
                width: fit-content;
                font-size: 45rpx;
                color: #000000;
                font-weight: bold;
                margin-bottom: 35rpx;
            }
            >.one{
                border: 1px solid #dfeaea;
                border-radius: 5rpx;
                padding: 25rpx;
                box-sizing: border-box;
                display: grid;
                grid-gap: 35rpx;
                font-size: 30rpx;
                margin-bottom: 35rpx;
                >.top{
                    display: grid;
                    grid-template-columns: 1fr 1fr;
                }
                >.top_one{
                    display: flex;
                    align-items: center;
                }
                >.content{
                    display: grid;
                    grid-template-columns: 1fr 2fr;
                }
                >.bottom{
                    display: grid;
                    grid-template-columns: 1fr 1fr 1fr;
                    grid-gap: 15px;
                }
            }
            >.three{
                margin-bottom: 35rpx;
                .table_container{
                    max-height: 40vh;
                    overflow-y: scroll;
                }
            }
            >.two{
                .table_container{
                    max-height: 40vh;
                    overflow-y: scroll;
                }
            }
        }
    }
</style>
